﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Base;

namespace ConAppCore
{
    internal class TestTimeEX
    {
        public static void Test()
        {
            //TestWinInstallTime();
            //TestTikmestmp();

            CPPValidte();
            //DateTimeAdd();
            TestSubtract();
        }

        private static void TestWinInstallTime()
        {
            DateTime installTime = new DateTime(2024, 11, 25, 13, 11, 01, DateTimeKind.Local);

            var timestampFactory = new TicksTimestampFactory(new DateTime(1601, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), TimestampUnit.Nanosecond100);
            var InstallTime = timestampFactory.GetTimestamp(installTime);

            var timestampFactory2 = new TicksTimestampFactory(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), TimestampUnit.Second);
            var InstallDate = timestampFactory2.GetTimestamp(installTime);
        }

        private static void TestSubtract()
        {
            System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
            System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);

            // diff1 gets 185 days, 14 hours, and 47 minutes.
            System.TimeSpan diff1 = date2.Subtract(date1);

            // date4 gets 4/9/1996 5:55:00 PM.
            System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);
            System.DateTime date4 = date3.Subtract(diff1);


            var time1 = new DateTime(2001, 1, 31, 23, 0, 0, 0, DateTimeKind.Local);
            var time3 = new DateTime(2022, 3, 14, 16, 20, 30, 15, DateTimeKind.Local);
            TimeSpan time32 = time3.Subtract(time1);
        }

        private static void DateTimeAdd()
        {
            var time1 = new DateTime(2001, 1, 31, 23, 0, 0, 0, DateTimeKind.Local);
            var time11 = time1.AddMonths(1);
            var time12 = time1.AddDays(32);

            var time2 = new DateTime(2001, 5, 31, 0, 0, 0, 0, DateTimeKind.Local);
            var time122 = time2.AddMonths(1);



            //var ts = new TimeSpan();
            //TimeSpan.FromDays();
            //TimeSpan.FromHours();
            //TimeSpan.FromMinutes();
            //TimeSpan.FromSeconds();
            //TimeSpan.FromMilliseconds();
            //TimeSpan.FromTicks();

        }


        private static void CPPValidte()
        {

            //string time11 = Seconds(1662621208);
            //string time12 = Seconds(1662625020);
            string time3 = XXX(16631259245852367);




            var refTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Local);
            //TimeEx.TimestampFactory = new TimestampFactory(refTime, unit);
            //TimeEx.TimestampFactory = new TicksTimestampFactory(refTime, TimestampUnit.Nanosecond100);
            //var ttt = TimeEx.TimestampToDateTime(1660568088000000000 / 100, DateTimeKind.Utc);
            //var ttt2 = TimeEx.TimestampToDateTime(1663403481926655400 / 100, DateTimeKind.Utc).ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss.fffffff");

            var ttt3 = TimeEx.TimestampToDateTime(1663403583850, DateTimeKind.Utc).ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss.fffffff");

            //ttt3.ToLocalTime();
            //ttt3.ToUniversalTime();
            //ttt3.Add(TimeSpan.FromDays());
            //ttt3.AddYears();
            //ttt3.AddMonths();
            //ttt3.AddDays();
            //ttt3.AddHours();
            //ttt3.AddMinutes();
            //ttt3.AddSeconds();
            //ttt3.AddMilliseconds();
            //ttt3.AddTicks();

            //ttt3.Subtract();
        }


        public static void show(int a, params Object[] change)
        {
            Console.WriteLine("正常的参数：" + a);
            for (int i = 0; i < change.Length; i++)
            {
                Console.WriteLine(change[i].ToString());
            }

            var time = DateTime.Now;
            var ts = TimeSpan.FromMilliseconds(123);//FromDays-FromHours--FromMinutes-FromSeconds
            var time2 = time.Add(ts);
        }



        //private static string Seconds(double ts)
        //{
        //    var reefTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        //    var time = reefTime.AddSeconds(ts).ToLocalTime();
        //    return time.ToString("yyyy-MM-dd HH:mm:ss.fffffff");
        //}

        private static string XXX(long ts)
        {
            const long OFSSET = 3600 * 1000 * 8;//C#的Datetime的Ticks，以格林尼治时间为准，而我们本地的时间是北京时间，比如本地时间的比如 2018/12/26 0:0:0，并不等于格林尼治时间的比如 2018/12/26 0:0:0，而是恰好差了八个小时
            DateTime dateTime = TimeEx.TimestampToDateTime(ts / 10000 + OFSSET, DateTimeKind.Utc);
            DateTime dateTime2 = dateTime.ToLocalTime();
            return dateTime2.ToString("yyyy-MM-dd HH:mm:ss.fffffff");

            //var reefTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            //var time = reefTime.AddMilliseconds(ts).ToLocalTime();
            //return time.ToString("yyyy-MM-dd HH:mm:ss.fffffff");
        }



        private static void TestTikmestmp()
        {
            var time = new DateTime(2022, 9, 14, 14, 58, 23, 105, DateTimeKind.Local);
            TimestampUnit unit = TimestampUnit.Nanosecond100;
            //unit = TimestampUnit.Millisecond;

            var refTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Local);
            var factory = new TimestampFactory(refTime, unit);
            var factory2 = new TicksTimestampFactory(refTime, unit);
            //TimeEx.TimestampFactory = new TimestampFactory(refTime, unit);
            TimeEx.TimestampFactory = new TicksTimestampFactory(refTime, unit);

            long ts = factory.GetTimestamp(time);
            DateTime ret11 = factory.TimestampToDateTime(ts);
            DateTime ret12 = factory2.TimestampToDateTime(ts);
            DateTime ret13 = TimeEx.TimestampToDateTime(ts);

            long ts2 = factory2.GetTimestamp(time);
            DateTime ret21 = factory.TimestampToDateTime(ts2);
            DateTime ret22 = factory2.TimestampToDateTime(ts2);
            DateTime ret23 = TimeEx.TimestampToDateTime(ts2);

            long ts3 = TimeEx.GetTimestamp(time);
            DateTime ret31 = factory.TimestampToDateTime(ts3);
            DateTime ret32 = factory2.TimestampToDateTime(ts3);
            DateTime ret33 = TimeEx.TimestampToDateTime(ts3);
        }

        private static void TmpCode()
        {
            //var ts = TimeEx.GetTimestamp(new DateTime(9999, 12, 31, 23, 59, 59));
            //253402300800000
            //14757395258967641292
            //var tt = TimeEx.TimestampToDateTime(14757395258967641292);
            //var tt = TimeEx.TimestampToDateTime(147573952589676);

            //EventWaitHandle ew = new EventWaitHandle(false, EventResetMode.ManualReset);
            //ManualResetEvent mr = new ManualResetEvent(false);
            //mr.Reset();
            //CancellationTokenSource cts = new CancellationTokenSource();
            //cts.Cancel();
            //cts.Token.WaitHandle.WaitOne();
            ////cts.Token.IsCancellationRequested
            //Type type;

            //Encoding.ASCII
            //Encoding.Unicode
            //Encoding.UTF32
            //Encoding.UTF7
            //Encoding.GetEncoding(int codepage);
            //Encoding.UTF8.GetString();
            //Encoding.UTF8.GetBytes();
            //Encoding.GetEncodings();

            //DateTime dt = DateTime.Now;
            //DateTime dt2 = new DateTime(2022, 12, 15, 23, 45, 32);
            //TimeSpan.FromMilliseconds

            //string start = XXX(256760210267700);
            //string end = XXX(256763223070400);

            //string time1 = XXX(16626204748167117);
            //string time2 = XXX(16626205871294530);
        }
    }
}
